﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WeifenLuo.WinFormsUI.Docking;
using CustomUIControls;


namespace SalesVMT
{
    public partial class frmOderPurchaseList : DockContent
    {
        int pageSize = UIParamater.PageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        public frmOderPurchaseList()
        {
            InitializeComponent();
            mtxFromDate.Text = DateTime.Now.AddMonths(-1).ToString("dd/MM/yyyy");
            mtxToDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
            
            DisplayFrame();
            UISetting.LoadProviderFromReceiptForReport(ref cboProvider);
            cboProvider.SelectedValue = 0;
        }

        private void DisplayFrame()
        {
            UISetting.AddColumn(ref dgvList1, "Receipt_ID", "Khóa", "", 1, false, true);
            UISetting.AddColumn(ref dgvList1, "RowNumber", "STT", "C", 50, true, true);
            UISetting.AddColumn(ref dgvList1, "Provider_ID", "Mã NCC", "", 130, false, true);
            UISetting.AddColumn(ref dgvList1, "Provider_Code", "Mã NCC", "", 130, true, true);
            UISetting.AddColumn(ref dgvList1, "Provider_Name", "Nhà cung cấp", "", 300, true, true);
            UISetting.AddColumn(ref dgvList1, "Receipt_Code", "Phiếu NK", "", 130, true, true);
            UISetting.AddColumn(ref dgvList1, "Receipt_Date", "Ngày nhập", "C", 110, true, true);

            UISetting.AddColumn(ref dgvList1, "Receipt_TotalPrice", "Tiền hàng", "R", 130, true, true);
            UISetting.AddColumn(ref dgvList1, "Receipt_TotalDownPrice", "Giảm giá", "R", 130, true, true);
            UISetting.AddColumn(ref dgvList1, "Receipt_TotalVAT", "VAT", "R", 100, true, true);
            UISetting.AddColumn(ref dgvList1, "Receipt_Total", "Tổng cộng", "R", 130, true, true);
        }
        private void LoadData()
        {
            int ProviderID = Convert.ToInt32(cboProvider.SelectedValue);
            DateTime FromDate = DateTime.ParseExact(mtxFromDate.Text, "dd/MM/yyyy", null);
            DateTime ToDate = DateTime.ParseExact(mtxToDate.Text, "dd/MM/yyyy", null);

            DataTable dt = ReceiptDB.GetOrderPurchase(out itemCount, ProviderID, FromDate, ToDate, currPage, pageSize).Tables[0];
            PageIndex();
            dgvList1.Rows.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dgvList1.Rows.Insert(i, 1);
                dgvList1.Rows[i].Cells["Receipt_ID"].Value = dt.Rows[i]["Receipt_ID"].ToString();
                dgvList1.Rows[i].Cells["Provider_ID"].Value = dt.Rows[i]["Provider_ID"].ToString();
                dgvList1.Rows[i].Cells["Provider_Code"].Value = dt.Rows[i]["Provider_Code"].ToString();
                dgvList1.Rows[i].Cells["Provider_Name"].Value = dt.Rows[i]["Provider_Name"].ToString();
                dgvList1.Rows[i].Cells["Receipt_Code"].Value = dt.Rows[i]["Receipt_Code"].ToString();
                dgvList1.Rows[i].Cells["Receipt_Date"].Value = Convert.ToDateTime(dt.Rows[i]["Receipt_Date"]).ToString("dd/MM/yyyy");
                dgvList1.Rows[i].Cells["RowNumber"].Value = dt.Rows[i]["RowNumber"].ToString();
                dgvList1.Rows[i].Cells["Receipt_TotalPrice"].Value =Convert.ToDecimal(dt.Rows[i]["Receipt_TotalPrice"]).ToString("N1");
                dgvList1.Rows[i].Cells["Receipt_TotalDownPrice"].Value = Convert.ToDecimal(dt.Rows[i]["Receipt_TotalDownPrice"]).ToString("N1");
                dgvList1.Rows[i].Cells["Receipt_TotalVAT"].Value = Convert.ToDecimal(dt.Rows[i]["Receipt_TotalVAT"]).ToString("N1");
                dgvList1.Rows[i].Cells["Receipt_Total"].Value = Convert.ToDecimal(dt.Rows[i]["Receipt_Total"]).ToString("N1");
                
            }
        }
        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }
        
        private void btnFirst_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage != 0)
                currPage--;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (totalPage > 0 && currPage != totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (dgvList1.SelectedRows.Count > 0)
            {
                string ProviderID = dgvList1.SelectedRows[0].Cells["Provider_ID"].Value.ToString();
                frmReport f = new frmReport("DON DAT HANG DA MUA", new string[] { ProviderID, mtxFromDate.Text, mtxToDate.Text });
                f.Show(this);
            }

        }

        private void cboProvider_SelectedIndexChanged(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

    }
}